<?php
// +-------------------------------------------------------------+
// | Tinysupe [ SIMPLE IS THE BEST ! ]
// +-------------------------------------------------------------+
// | Copyright (c) 2011 www.srccn.com All rights reserved.
// +-------------------------------------------------------------+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-------------------------------------------------------------+
// | Author: tinysu <tinysu@qq.com>
// +-------------------------------------------------------------+

class mysql
{
	private $conn;
	public $dburl, $charset, $timezone;

	function __construct($dburl=null) {
		$this->conn = null;
		$this->charset = 'utf8';
		$this->timezone = '+8:00';
		if (!is_null($dburl)) $this->open($dburl);
	}

	function __destruct(){
		if($this->conn) $this->close();
	}

	public function open($dburl='') {
		if (is_null($this->conn)) {
			if ($dburl!='') $this->dburl = $dburl;
			$dburl = parse_url($this->dburl);
			if ($dburl['scheme'] == 'mysql') {
				if(isset($dburl['port'])) $dburl['host'] .= ':'. $dburl['port'];
				$this->conn = mysql_connect($dburl['host'], $dburl['user'], $dburl['pass']) or die('MySQL Connect Error!');
    			mysql_select_db(substr($dburl['path'], 1), $this->conn);
				mysql_query('set time_zone = \''.$this->timezone.'\'', $this->conn);
				mysql_query('set names '. $this->charset, $this->conn);
			}
		}
		return $this->conn;
	}
	
	public function query($sql){
		$sql = $this->query_sql($sql);
		return mysql_query($sql, $this->conn);
	}

	public function query_one($sql){
		$one = false;
		if ($res = $this->query($sql)) {
			if($row = mysql_fetch_array($res)){
				$one = $row[0];
			}
			mysql_free_result($res);
		}
		return $one;
	}
	
	public function query_row($sql){
		$row = false;
		if ($res = $this->query($sql)) {
			$row = mysql_fetch_assoc($res);
			mysql_free_result($res);
		}
		return $row;
	}
	
	public function query_all($sql){
		$all = array();
		if ($res = $this->query($sql)) {
			while($row = mysql_fetch_assoc($res)){
				$all[] = $row;
			}
			mysql_free_result($res);
		}
		return $all;
	}
	
	public function query_page($sql, $pnum=1, $psize=20){
		$pnum = intval($pnum);
		$psize = intval($psize);
		
		$sql = $this->query_sql($sql);
		$total = $this->query_one(preg_replace('/select(.+?)from/i', 'select count(*) from', $sql, 1));

		if ($psize==0) $psize = 20;
		$pcnt = ceil($cnt / $psize);
		
		if ($pnum>$pcnt) $pnum = $pcnt;
		if ($pnum<1) $pnum = 1;

		$sql .= ' limit '.(($pnum-1)*$psize).','.$psize;
			
		$ret = array();
		$ret['rows'] = $this->query_all($sql);
		$ret['total'] = $total;
		$ret['psize'] = $psize;
		$ret['pcnt'] = $pcnt;
		$ret['pnum'] = $pnum;
		$ret['psta'] = $pnum-5>0 ? $pnum-5 : 1;
		$ret['pend'] = $ret['psta']+9<$pcnt ? $ret['psta']+9 : $pcnt;
		$ret['psta'] = $ret['pend']-9>0 ? $ret['pend']-9 : 1;
	
		return $ret;
	}

	private function query_sql($sql){
		if (is_string($sql)) {
			return $sql;
		} elseif(is_array($sql)) {
			$str = '';
			foreach ($sql as $key => $val) {
				if (is_array($val)) {
					$val = implode(', ', $val);
					if (strtolower($key) == 'values') {
						$val = '('. $val .')';
					}
				}
				$str .= $key.' '.$val.' ';
			}
			return $str;
		}
	}

	public function check(){
		foreach ($_GET as $key => $val) { 
			if (is_numeric($val)) { 
				$_GET[$key] = intval($val); 
			} else { 
				$_GET[$key] = $this->escape($val); 
			}
		} 
		foreach ($_POST as $key => $val) { 
			if (is_numeric($val)) { 
				$_POST[$key] = intval($val); 
			} else { 
				$_POST[$key] = $this->escape($val); 
			}
		}
	}

	public function escape($str){
		if (is_array($str)) {
			foreach ($str as $key => $val)
				$str[$key] = $this->escape($val);
		} elseif (!get_magic_quotes_gpc()) {
			$str = addslashes($str);
		}
		return trim($str);
	}
	
	public function close() {
		if ($this->conn) {
			@mysql_close($this->conn);
		}
	}
}
?>